package com.heima.article.listener;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.article.mapper.ArticleConfigMapper;
import com.heima.model.article.pojos.ApArticleConfig;
import com.heima.model.common.constants.WmNewsMessageConstants;
import com.heima.model.wemedia.pojos.WmNews;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class DownOrUpListener {

    @Autowired
    private ArticleConfigMapper articleConfigMapper;
    /*
    监听文章上下架功能
     */
   @RabbitListener(
           bindings = @QueueBinding(
                   value = @Queue(name = WmNewsMessageConstants.WM_NEWS_UP_OR_DOWN_QUEUE,durable = "true"),
                   exchange = @Exchange(name = WmNewsMessageConstants.WM_NEWS_UP_OR_DOWN_TOPIC,type = ExchangeTypes.TOPIC),
                   key = WmNewsMessageConstants.WM_NEWS_UP_OR_DOWN_ROUTINGKEY
           )
   )
    public void getMessage(WmNews wmNews){
       ApArticleConfig apArticleConfig = new ApArticleConfig();
       apArticleConfig.setArticleId(wmNews.getArticleId());
       apArticleConfig.setIsDown(wmNews.getEnable() == 1 ? true : false);
       articleConfigMapper.update(apArticleConfig,Wrappers.<ApArticleConfig>lambdaUpdate()
               .eq(ApArticleConfig::getArticleId, wmNews.getArticleId())
               .set(ApArticleConfig::getIsDown,apArticleConfig.getIsDown())
       );
       log.info("dui:{}",apArticleConfig);
    }

}
